<?php

/**
	 * Convert time to ago format
	 *
	 * @param unknown_type $time
	 * @param unknown_type $language
	 * @return unknown
	 */
	function time_ago_vn($time, $str)
	{	
		$timeDiff = time() - $time;
		$strTime = '';
		if ($timeDiff < 60)
		{
		    return $str;
		}
		else 
		{
		    $strTime = '';
		    if ($timeDiff < 3600)
		    {
		        $min = round($timeDiff/60);
		        return $strTime.$min.' phút trước';
		    }
		    else if ($timeDiff < 86400)
	        {
	            $hour = round($timeDiff/3600);
	            return $strTime.$hour.' giờ trước';
	        }
	        else if ($timeDiff < 2592000)
	        {
	            $day = round($timeDiff/86400);
	            return $strTime.$day.' ngày trước';
	        }
	        else if ($timeDiff < 31536000)
	        {
	            $month = round($timeDiff/2592000);
	            return $strTime.$month.' tháng trước';
	        }
	        else 
	        {
	            $year = round($timeDiff/31536000);
	            return $strTime.$year.' năm trước';
	        }	     
		}
		return '';
	}
	
	/**
	 * function to return age from $birthdate
	 *
	 * @param unknown_type $birthdate
	 * @return unknown
	 */
	function get_age($birthdate)
	{
		if ($birthdate == '')
		return '';
		 
		$dateArray = date_parse($birthdate);
		return date('Y') - $dateArray['year'] -
		(date('n') < (ltrim($dateArray['month'],'0') + (date('j') < ltrim($dateArray['day'],'0'))));
	}
	
	function get_playing_time($str)
	{	
	    $minute_length= floor($str/60);
	    $second_length = $str % 60;
	    if ($second_length < 10)
	    {
	        $second_string  = "0" . $second_length;
	    }
	    else
	    {
	        $second_string  = $second_length;
	    }
	
	    $minute_length = (empty($minute_length))? "00" : $minute_length;
	    $second_string = (empty($second_string))? "00" : $second_string;
	
	    if($minute_length == 0)
	    {
	        $minute_length = "00";
	    }
	    if(($minute_length) < 10 && $minute_length != "00")
	    {
	        return ("0".$minute_length.":".$second_string);
	    }
	    else
	    {
	        return ($minute_length.":".$second_string);
	    }
	}
	function convert_datetime($str) {
	    list($date, $time) = explode(' ', $str);
	    list($year, $month, $day) = explode('-', $date);
	    list($hour, $minute, $second) = explode(':', $time);
	    $timestamp = mktime($hour, $minute, $second, $month, $day, $year);
	    return $timestamp;
	} 
?>